/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-22
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Bom;
import com.rzico.basics.entity.BomItem;
import com.rzico.basics.entity.Goods;
import com.rzico.basics.entity.Product;
import com.rzico.basics.mapper.BomItemMapper;
import com.rzico.basics.mapper.ProductMapper;
import com.rzico.exception.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.BomMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <pre>
 * 商品清单业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class BomService extends BaseServiceImpl<Bom, String> {

    @Autowired
    private BomMapper bomMapper;

    @Autowired
    private BomItemMapper bomItemMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public BaseMapper<Bom, String> getMapper() {
        return bomMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public int save(Bom bom) {

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",bom.getEnterpriseId());
        params.put("productId",bom.getProductId());
        List<Bom> bomList = bomMapper.selectList(params);
        if (bomList.size()>0) {
            bom.setId(bomList.get(0).getId());
        }

        if (bom.getId()!=null) {
            BomItem bi = new BomItem();
            bi.setBomId(bom.getId());
            bomItemMapper.delete(bi);
        }

        int w = 0;
        if (bom.getId()==null) {
            w = bomMapper.insertUseGeneratedKeys(bom);
        } else {
            bom.setDeleted(false);
            w = bomMapper.updateByPrimaryKeySelective(bom);
        }

        for (BomItem bomItem:bom.getItemList()) {
            bomItem.setBomId(bom.getId());
            bomItemMapper.insertUseGeneratedKeys(bomItem);
        }

        Product product = productMapper.selectByPrimaryKey(bom.getProductId());
        product.setSubType(bom.getType());
        productMapper.updateByPrimaryKeySelective(product);


        return w;

    }



    @Transactional(rollbackFor = Exception.class)
    public int update(Bom bom) {

        int w = bomMapper.updateByPrimaryKeySelective(bom);

        BomItem bi = new BomItem();
        bi.setBomId(bom.getId());
        bomItemMapper.delete(bi);

        for (BomItem bomItem:bom.getItemList()) {
            bomItem.setBomId(bom.getId());
            bomItemMapper.insertUseGeneratedKeys(bomItem);
        }


        Product product = productMapper.selectByPrimaryKey(bom.getProductId());
        product.setSubType(bom.getType());
        productMapper.updateByPrimaryKeySelective(product);

        return w;

    }

    @Transactional(rollbackFor = Exception.class)
    public Bom addWater(Product product,Long productCategoryId) {
        Goods goods = new Goods();
        goods.setCreateDate(new Date());
        goods.setReview(0L);
        goods.setTotalSale(0L);
        goods.setHits(0L);
        int affectCount = goodsService.insertUseGeneratedKeys(goods);
        if (affectCount <= 0){
            throw new CustomException("保存商品主表失败");
        }

        Product prod = new Product();
        BeanUtils.copyProperties(product,prod);
        prod.setId(null);
        prod.setName("水票-"+prod.getName());
        prod.setGoodsId(goods.getId());
        prod.setType(1);
        prod.setSubType(2);
        prod.setIsList(true);

        prod.setMinLimit(0);
        prod.setFreightId(null);

        if (productCategoryId==null) {
            prod.setProductCategoryId(product.getProductCategoryId());
        } else {
            prod.setProductCategoryId(productCategoryId);
        }
        productMapper.insertUseGeneratedKeys(prod);


        Bom bom = new Bom();
        bom.setDeleted(false);
        bom.setCreateDate(new Date());
        bom.setEnterpriseId(product.getEnterpriseId());
        bom.setProductId(prod.getId());
        bom.setProduct(prod);
        bom.setQuantity(1);
        bom.setType(2);

        List<BomItem> itemList = new ArrayList<>();
        BomItem bi = new BomItem();
        bi.setConvRate(BigDecimal.ONE);
        bi.setProductId(product.getId());
        bi.setProduct(product);
        itemList.add(bi);
        bom.setItemList(itemList);

        save(bom);

        return bom;

    }

    public Boolean checkCanUse(Long productId,Long itemProductId ) {

        Map<String,Object> params = new HashMap<>();
        params.put("type",2);
        params.put("productId",productId);
        List<Bom> bomList = bomMapper.selectList(params);
        if (bomList.size()==0) {
            return false;
        }

        params.clear();
        params.put("bomId",bomList.get(0).getId());
        List<BomItem> bomItemList = bomItemMapper.selectList(params);
        for (BomItem bomItem:bomItemList) {
            if (bomItem.getProductId().equals(itemProductId)) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {

        int rw = 0;
        for (Object id:ids) {

            Bom bom = selectByPrimaryKey(id);

            Product product = productMapper.selectByPrimaryKey(bom.getProductId());
            if (bom.getType().equals(2)) {
                product.setDeleted(true);
            } else {
                product.setSubType(0);
            }
            productMapper.updateByPrimaryKeySelective(product);

            bom.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(bom);

        }
        return rw;

    }

}
